import Vue from 'vue'
import moment from 'moment'
import 'moment/locale/zh-cn'
moment.locale('zh-cn')

Vue.filter('NumberFormat', function (value) {
  if (!value) {
    return '0'
  }
  const intPartFormat = value.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,') // 将整数部分逢三一断
  return intPartFormat
})

Vue.filter('dayjs', function (dataStr, pattern = 'YYYY-MM-DD HH:mm:ss') {
  return moment(dataStr).format(pattern)
})

Vue.filter('moment', function (dataStr, pattern = 'YYYY-MM-DD HH:mm:ss') {
  return moment(dataStr).format(pattern)
})

Vue.filter('timeToStr', function (dataStr, pattern = 'YYYY-MM-DD HH:mm:ss') {
  return moment(dataStr*1000).format(pattern)
})

Vue.filter('statusFilter', function (val, statusList, text) {
  let index = statusList.findIndex(v=>v.id == val)
  if (index !== -1) {
    return statusList[index][text]
  } else {
    return '--'
  }
})

/**
 *
 * @param {string} a_name //文件名称
 */
Vue.filter('filenameHidden', function (a_name) {
  const nameArr = a_name.split('_')
  const fileArr = a_name.split('.')

  const fileType = fileArr.length > 1 ? fileArr[fileArr.length - 1] : ''
  const name = nameArr[0]
  // let fileType = nameArr[1] ?nameArr[1].split('.') : nameArr[0].split('.')

  return `${name}.${fileType}`
})



/**
 * Number formatting
 * like 10000 => 10k
 * @param {number} num
 * @param {number} digits
 */
Vue.filter('numberFormatter', function (num, digits) {
  const si = [{
      value: 1E18,
      symbol: 'E'
    },
    {
      value: 1E15,
      symbol: 'P'
    },
    {
      value: 1E12,
      symbol: 'T'
    },
    {
      value: 1E9,
      symbol: 'G'
    },
    {
      value: 1E6,
      symbol: 'M'
    },
    {
      value: 1E3,
      symbol: 'k'
    }
  ]
  for (let i = 0; i < si.length; i++) {
    if (num >= si[i].value) {
      return (num / si[i].value).toFixed(digits).replace(/\.0+$|(\.[0-9]*[1-9])0+$/, '$1') + si[i].symbol
    }
  }
  return num.toString()
})